package com.sx.zongzhi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.SelectPersonActivity;
import com.sx.zongzhi.adapter.InsideContactAdapter;
import com.sx.zongzhi.bean.ContactBean;
import com.sx.zongzhi.presenter.ContactPest;
import com.sx.zongzhi.utils.AppConstants;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IContactView;
import com.sx.zongzhi.widget.ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XZY on 2017/11/28.
 * 站内联系人
 */
public class InsideContactFragment extends Fragment implements IContactView, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.et_search)
    ClearEditText et_search;

    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

//    @BindView(R.id.list_View)
//    ListView list_View;
    public List<ContactBean> contactList = new ArrayList<>();
    private InsideContactAdapter contactAdapter;

    private String word_search = "";
    private int  page = 1,rows = 10;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_inside_contact,container,false);
        ButterKnife.bind(this,view);
        et_search.addTextChangedListener(textWatcher);
        initList();
        getData();
        return view;
    }

    private void initList() {
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeResources(AppConstants.colors);
            swipeRefreshLayout.setOnRefreshListener(this);
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        contactAdapter = new InsideContactAdapter(getActivity(),R.layout.item_contact_list,contactList);
        contactAdapter.setEmptyView(View.inflate(getActivity(), R.layout.load_error_view, null));
        recyclerView.setAdapter(contactAdapter);
        contactAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getData();
            }
        }, recyclerView);

    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(false);
        page = 1;
        getData();
    }
    private void getData() {
        new ContactPest(this).getContactList(getActivity(),word_search,page,rows);
    }

    @Override
    public void getContactListSuccess(List<ContactBean> contactBeanList) {

        if (page == 1) {
            contactList.clear();
            contactAdapter.setNewData(contactList);
        }
        if (contactBeanList != null){
            contactList.addAll(contactBeanList);
            List<ContactBean> selectList =((SelectPersonActivity)getActivity()).contactBeanList;
            if (selectList != null && selectList.size() > 0){
                for (int i = 0 ; i < contactList.size() ; i ++){
                    for (int j = 0 ; j < selectList.size() ; j ++){
                        if (contactList.get(i).id.equals(selectList.get(j).id)){
                            contactList.get(i).setSelect(true);
                        }
                    }
                }
            }
        }
        if (contactList.size() >= rows) {
            contactAdapter.loadMoreEnd();
        }else {
            contactAdapter.loadMoreComplete();
        }
        contactAdapter.notifyDataSetChanged();
    }
    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void onDestroyView() {
        if (contactAdapter != null){
            contactAdapter = null;
        }
        super.onDestroyView();
    }

    TextWatcher textWatcher=new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        @Override
        public void afterTextChanged(Editable s) {
            page = 1;
            word_search = s.toString().trim();
            getData();
        }
    };


}
